package com.zaaach.citypicker;

import android.os.Bundle;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.zaaach.citypicker.model.City;

import java.util.ArrayList;
import java.util.List;

public class ZoneDialog extends AbsDialogFragment {

    private RecyclerView mRecyclerView;

    private ZoneAdapter mAdapter;

    private List<City> extraListData = new ArrayList<>();

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_zone;
    }

    @Override
    protected int getDialogStyle() {
        return R.style.dialog;
    }

    @Override
    protected boolean canCancel() {
        return true;
    }

    @Override
    protected void setWindowAttributes(Window window) {
        window.setWindowAnimations(R.style.bottomToTopAnim);
        WindowManager.LayoutParams params = window.getAttributes();
        params.width = WindowManager.LayoutParams.MATCH_PARENT;
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        params.gravity = Gravity.BOTTOM;
        window.setAttributes(params);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        Bundle bundle = getArguments();
        if (bundle == null)
            return;

        mRecyclerView = findViewById(R.id.recyclerView);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));

        extraListData = bundle.getParcelableArrayList("extra_list_data");

        mAdapter = new ZoneAdapter(mContext, extraListData);
        if (mRecyclerView != null)
            mRecyclerView.setAdapter(mAdapter);

        mAdapter.setOnItemClickListener(new OnItemClickListener<City>() {
            @Override
            public void onItemClick(City bean, int position) {
                if (mActionListener != null)
                    mActionListener.onCityBean(bean);
                getDialog().cancel();
            }
        });

        findViewById(R.id.btn_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getDialog().cancel();
            }
        });
    }

    private ActionListener mActionListener;

    public void setActionListener(ActionListener actionListener) {
        mActionListener = actionListener;
    }


    public interface ActionListener {

        void onCityBean(City city);
    }
}
